#include "mainwindow.h"
#include "ui_mainwindow.h"

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    app = new App;

    connect(ui->buttonLoadFile, SIGNAL(clicked()), this, SLOT(loadFileButton()));
    connect(ui->buttonComputation, SIGNAL(clicked()), this, SLOT(runButton()));
}

MainWindow::~MainWindow()
{
    delete app;
    app = 0;

    delete ui;
}

 /*
  * Slots and signals below
  *
  */

void MainWindow::loadFileButton()
{
    QString filename = QFileDialog::getOpenFileName(this, trUtf8("nazwa"), ".");
    if(QFile::exists(filename))
    {
        // File exists
        ui->lineLoadFile->setStyleSheet("background-color: white;");
        ui->lineLoadFile->setText(filename);
        app->setFilename(filename);
    }
}

void MainWindow::runButton()
{
    // Let's start computation but before it will be possible, some configuration sets are require
    app->setSessionDuration(ui->spinSessionTime->value());
    app->setPopularPagesSize(ui->spinPopularPagesSize->value());
    app->setOneHostOneUser(ui->checkOneHostOneUser->isChecked());

    // Some security check
    if(ui->lineLoadFile->text() == "")
    {
        ui->lineLoadFile->setStyleSheet("background-color: red;");
        QMessageBox::critical(this, trUtf8("Błąd"), trUtf8("Przed rozpoczęciem przetwarzania danych,\nnależy wczytać odpowiedni plik!"));
    }
    else
    {
        // Let's gooooo!
        app->run();
    }
}
